import 'package:base_flutter/base_flutter.dart';
import 'package:ls_work/model/food_sample/food_sample_submit_data.dart';
import 'package:ls_work/net/post_request.dart';
import 'package:ls_work/net/response/food_sample/food_sample_detail_response.dart';

import '../../net/food_sample_api.dart';
import '../../net/response/food_sample/food_sample_list_data.dart';

class FoodSampleModel extends BaseMvvmModel {
  @override
  void onCleared() {}

  ///获取留样列表
  Future<List<FoodSampleListData>> getSampleList() async {
    var map = {};
    var data =
        await HttpGo.instance.postData(FoodSampleApi.foodSampleList, data: map);
    if ("0" == data['code']) {
      var list = (data['data']['rows'] as List ?? [])
          .map((e) => FoodSampleListData.fromJson(e))
          .toList();
      return list;
    } else {
      throw data['msg'];
    }
  }

  ///留样登记
  Future<Map> submitSample(FoodSampleSubmitData data) async {
    var map = data.toJson();
    var result = await HttpGo.instance
        .postData(FoodSampleApi.createFoodSample, data: map);
    if ("0" == result['code']) {
      return result;
    } else {
      throw result['msg'];
    }
  }

  ///留样详情
  Future<FoodSampleDetailResponse> sampleDetail(dynamic id) async {
    var map = {"id": id};
    var data =
        await HttpGo.instance.getData(FoodSampleApi.sampleDetail, data: map);
    return FoodSampleDetailResponse.fromJson(data);
  }

  ///删除留样
  Future<Map> deleteSample(String id) async {
    var map = {"id": id};
    var result = await HttpGo.instance.postData(FoodSampleApi.deleteSample,
        data: map.toJsonMapStr(), options: PostRequest.urlencodedHeader);
    if ("0" == result['code']) {
      return result;
    } else {
      throw result['msg'];
    }
  }
}
